package com.propertypoint.presentation.actions;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.propertypoint.exception.DatabaseException;
import com.propertypoint.presentation.model.Agent;
import com.propertypoint.presentation.model.BaseUser;
import com.propertypoint.service.business.RegistrationServiceImpl;
import com.propertypoint.service.general.RegistrationService;
import com.propertypoint.util.MD5Util;
import com.propertypoint.util.PropertyPointConstants;

/**
 * Servlet implementation class RegistrationServlet
 */
@WebServlet("/RegistrationServlet")
public class RegistrationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private RegistrationService registrationService = new RegistrationServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RegistrationServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String email = request.getParameter(PropertyPointConstants.EMAIL);
		String password = request.getParameter(PropertyPointConstants.PASSWORD);
		String confirmPassword = request
				.getParameter(PropertyPointConstants.CONFIRM_PASSWORD);
		String firstName = request
				.getParameter(PropertyPointConstants.FIRST_NAME);
		String lastName = request
				.getParameter(PropertyPointConstants.LAST_NAME);
		String phoneNumber = request
				.getParameter(PropertyPointConstants.PHONE_NUMBER);
		
		String dob = request.getParameter(PropertyPointConstants.DOB);
		
		String postalAddress = request
				.getParameter(PropertyPointConstants.POSTAL_ADDRESS);
		String zipCode = request.getParameter(PropertyPointConstants.ZIP_CODE);
		String gender = request.getParameter(PropertyPointConstants.GENDER);
		String licenseNo = request
				.getParameter(PropertyPointConstants.LICENSE_NO);
		String agent = request.getParameter(PropertyPointConstants.AGENCY);
		BaseUser user = new Agent(licenseNo, agent);
		 
		try {
			user.setEmail(email);
			user.setUsername(email);
			user.setPassword(MD5Util.getMD5String(password));
			user.setFirstName(firstName);
			user.setLastName(lastName);
			user.setPhoneNumber(phoneNumber);
			user.setAddress(postalAddress);
			user.setDateOfBirth(dob);
			user.setZipCode(zipCode);
			user.setGender(gender);
			registrationService.register(user);
			request.setAttribute("reg_status", "true");
			request.setAttribute("reg_message", "You have successfully registered for Property Point !");
			
		} 
		catch (DatabaseException e1) {
			request.setAttribute("reg_error", "User Already Registered!");
			request.setAttribute("reg_status", "false");
		}
		catch (Exception e1) {
			request.setAttribute("reg_error", e1.toString());
			request.setAttribute("reg_status", "false");
		}
		RequestDispatcher rd = getServletContext().getRequestDispatcher(
				PropertyPointConstants.REGISTRATION_PAGE);
		rd.forward(request, response);
	}

}
